package com.laxcen.smoke.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.dao.OperationDao;
import com.laxcen.smoke.dto.res.OperaterecordExtraRes;
import com.laxcen.smoke.service.OperationService;
import com.laxcen.smoke.util.DBUtils;
import com.laxcen.smoke.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class OperationServiceImpl implements OperationService {

    @Autowired
    private OperationDao operationDao;

    @Override
    public Response insert(String smartno, String boxno, String opname, Integer optype, String video, String orderno, String remark) {
        long ids = DBUtils.nextId();
        String id = String.valueOf(ids);
        String opid = operationDao.getUserNameById(opname);
        Date date = new Date();
        operationDao.insert(id,smartno,boxno,opid,opname,date,date,optype,video,orderno,remark);
        long idsdetail = DBUtils.nextId();
        String iddetail = String.valueOf(idsdetail);
        operationDao.insertdetail(iddetail,opid,smartno,boxno,date,optype,remark,0,remark);
        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response<PageInfo<OperaterecordExtraRes>> getAllList(int pageNo, int pageSize, int roleflag, String province, String city, String area) {
        List<OperaterecordExtraRes> list = new LinkedList<>();
        Page page = PageHelper.startPage(pageNo, pageSize);

        if (roleflag==0){
            List<OperaterecordExtraRes> listall = operationDao.getAllList();
            for (int i = 0; i<listall.size();i++){
                list.add(i,listall.get(i));
            }

        }else if (roleflag==1){
            String place = null;
            if (!province.isEmpty()){
                place = province;
            }
            if (!city.isEmpty()){
                place =place+"/"+ city;
            }
            if (!area.isEmpty()){
                place = place+"/"+area;
            }
                List<OperaterecordExtraRes> listall = operationDao.getAllListByRole(place);
                 for (int i = 0; i<listall.size();i++){
                list.add(i,listall.get(i));
            }
        }
        Response<PageInfo<OperaterecordExtraRes>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        response.setOK();
        response.setData(pageInfo);
        return response;
    }

    @Override
    public Response<PageInfo<OperaterecordExtraRes>> getTabList(String orderno, String starttime, String endtime, int roleflag, String province, String city, String area, int pageNo, int pageSize) throws ParseException {
        Page page = PageHelper.startPage(pageNo, pageSize);
        List<OperaterecordExtraRes> list = new LinkedList<>();
        if (roleflag==0) {
            if ("".equals(orderno) && "".equals(starttime) && "".equals(endtime)) {
                list = operationDao.getAllList();
            } else if ("".equals(orderno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtime(datestart, dateend);
            } else if (!"".equals(orderno) && "".equals(starttime) && "".equals(endtime)) {
                list = operationDao.getListByOrder(orderno);
            } else if (orderno == null && starttime == null && endtime == null) {
                list = operationDao.getAllList();
            } else if (!"".equals(orderno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtimeandOrder(datestart, dateend, orderno);
            } else if (orderno == null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtime(datestart, dateend);
            } else if (orderno != null && starttime == null && endtime == null) {
                list = operationDao.getListByOrder(orderno);
            } else if (orderno != null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtimeandOrder(datestart, dateend, orderno);
            }
        }else if (roleflag==1) {
            String place = null;
            if (!province.isEmpty()){
                place = province;
            }
            if (!city.isEmpty()){
                place =place+"/"+ city;
            }
            if (!area.isEmpty()){
                place = place+"/"+area;
            }
            if ("".equals(orderno) && "".equals(starttime) && "".equals(endtime)) {
                list = operationDao.getAllListByRole(place);
            } else if ("".equals(orderno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtimeRole(datestart, dateend,place);
            } else if (!"".equals(orderno) && "".equals(starttime) && "".equals(endtime)) {
                list = operationDao.getListByOrderRole(orderno,place);
            } else if (orderno == null && starttime == null && endtime == null) {
                list = operationDao.getAllListByRole(place);
            } else if (!"".equals(orderno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtimeandOrderRole(datestart, dateend, orderno,place);
            } else if (orderno == null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtimeRole(datestart, dateend,place);
            } else if (orderno != null && starttime == null && endtime == null) {
                list = operationDao.getListByOrderRole(orderno,place);
            } else if (orderno != null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = operationDao.getListByOtimeandOrderRole(datestart, dateend, orderno,place);
            }
        }
        Response<PageInfo<OperaterecordExtraRes>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        response.setData(pageInfo);
        response.setOK();
        return response;
    }

    @Override
    public Response delete(String id) {
        operationDao.delete(id);
        operationDao.deletedetail(id);
        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response<OperaterecordExtraRes> getOne(String id) {
        OperaterecordExtraRes operaterecordExtraRes = operationDao.getOneById(id);
        Response<OperaterecordExtraRes> resResponse = new Response<>();
        resResponse.setOK();
        resResponse.setData(operaterecordExtraRes);
        return resResponse;
    }
}
